package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.entity.Items;
import com.cssl.entity.Option;
import com.cssl.entity.Subject;
import com.cssl.entity.Users;
import com.cssl.service.ItemsService;
import com.cssl.service.OptionService;
import com.cssl.service.SubjectService;
import com.cssl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@Transactional
public class VoteController {
    @Autowired
    private OptionService optionService;
    @Autowired
    private ItemsService itemsService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private UserService userService;

    @RequestMapping("/show/{index}/{title}")
    public String List(@PathVariable Integer index,@PathVariable String title, Model md){
        System.out.println("/show/index/title");

        if(index==null||index==0){
            index=1;
        }
        if(title.equals("null")){
            title="";
        }
        int sum=subjectService.list().size();
        int last=(sum%3)==0?(sum/3):(sum/3)+1;
        IPage<Items> page = itemsService.selectAll(new Page<>(index,3), title,0);

        md.addAttribute("title",title);
        md.addAttribute("last",last);
        md.addAttribute("index",index);
        md.addAttribute("page",page);
        return "votelist";
    }

    @RequestMapping("/show2/{index}")
    public String pgList(@PathVariable Integer index,String title, Model md){
        System.out.println("/show/index");
        if(index==null||index==0){
            index=1;
        }
        if(title.equals("null")){
            title="";
        }
        int sum=subjectService.list().size();
        int last=(sum%3)==0?(sum/3):(sum/3)+1;
        IPage<Items> page = itemsService.selectAll(new Page<>(index,3), title,0);

        md.addAttribute("title",title);
        md.addAttribute("last",last);
        md.addAttribute("index",index);
        md.addAttribute("page",page);
        return "votelist";
    }

    @RequestMapping("/view/{sid}")
    public String view(@PathVariable Integer sid, Model md){
        System.out.println("view");
        IPage<Items> page = itemsService.selectAll(new Page<>(1,3),"null", sid);
        List<Map<String, Object>> list= itemsService.selectId(sid);

        md.addAttribute("page",page);
        md.addAttribute("list",list);
        return "voteview";
    }

    @RequestMapping("/vote1/{sid}")
    public String vote1(@PathVariable Integer sid, Model md, HttpSession session){
        System.out.println("vote1");
        String use=(String) session.getAttribute("user");
        Users u=userService.getOne(new QueryWrapper<Users>().eq("username",use));
        List<Items> items=itemsService.list(new QueryWrapper<Items>().eq("i_sid",sid).eq("i_uid",u.getUid()));
        if(items.size()!=0){
            md.addAttribute("hid","         该投票你已参与");
            return "forward:/view/"+sid;
        }
        IPage<Items> page = itemsService.selectAll(new Page<>(1,3),"null", sid);
        List<Map<String, Object>> list= itemsService.selectId(sid);

        md.addAttribute("page",page);
        md.addAttribute("list",list);

        return "vote";
    }

    @RequestMapping("/votes/{sid}")
    public String votes(@PathVariable Integer sid, Integer[] options, Model md, HttpSession session){

        String use=(String) session.getAttribute("user");
        Users u=userService.getOne(new QueryWrapper<Users>().eq("username",use));
        List<Items> lists=new ArrayList<Items>();

        for (int option : options) {
            Items i=new Items();
            i.setI_oid(option);
            i.setI_sid(sid);
            i.setI_uid(u.getUid());
            System.out.println(option);
            System.out.println(sid);
            System.out.println(u.getUid());

            lists.add(i);
        }

        if(itemsService.saveBatch(lists)){
            return "forward:/view/"+sid;
        }
        IPage<Items> page = itemsService.selectAll(new Page<>(1,3),"null", sid);
        List<Map<String, Object>> list= itemsService.selectId(sid);

        md.addAttribute("page",page);
        md.addAttribute("list",list);
        return "vote";
    }
    @RequestMapping("/ad")
    public String ad(Model md){

        return "add";
    }

    @ResponseBody
    @RequestMapping("/pdSubject")
    public boolean pdSubject(String title){
        Subject sub=subjectService.getOne(new QueryWrapper<Subject>()
                .eq("title",title));
        if(sub==null){
            System.out.println("pdSubject");
            return true;
        }
        return false;
    }
    @RequestMapping("/addvote")
    public String addvote(Subject subject,String[] options, Model md){

        List<Option> lists=new ArrayList<Option>();

        if(subjectService.save(subject)){
            Subject sub=subjectService.getOne(new QueryWrapper<Subject>()
                .eq("title",subject.getTitle()));
            for (String option : options) {
                Option op=new Option();
                op.setContent(option);
                op.setO_sid(sub.getSid());
                System.out.println("op对象"+op);
                lists.add(op);
            }
            for (Option list : lists) {
                System.out.println(list);
            }
            boolean res=optionService.saveBatch(lists);
            if(res){
                return "forward:/show/1/null";
            }else {
                return "add";
            }
        }
        md.addAttribute("pd","添加失败");
        return "add";
    }

    @RequestMapping("/upd/{title}")
    public String upd(@PathVariable String title, Model md){

        Subject subject=subjectService.getOne(new QueryWrapper<Subject>().eq("title",title));
        System.out.println(subject.getSid());
        List<Map<String,Object>> list=itemsService.selectId(subject.getSid());
        md.addAttribute("subject",subject);
        md.addAttribute("option",list);

        return "upd";
    }

    @RequestMapping("/updvotes")
    public String updvotes(Subject subject,String[] options,String[] options2,Integer[] oid, Model md){
        List<Option> lists=new ArrayList<Option>();
        List<Option> lists2=new ArrayList<Option>();
        int i=0;
        if(subjectService.updateById(subject)){
            for (String option : options) {
                Option op=new Option();
                op.setOid(oid[i]);
                op.setContent(option);
                op.setO_sid(subject.getSid());
                lists.add(op);
                i++;
            }
            for (String option2 : options2) {
                Option op=new Option();
                op.setContent(option2);
                op.setO_sid(subject.getSid());
                lists2.add(op);
            }
            boolean addres=optionService.saveBatch(lists2);
            boolean res=optionService.updateBatchById(lists);
            if(res && addres){
                return "forward:/show/1/null";
            }else {
                return "upd";
            }
        }
        md.addAttribute("pd","修改失败");
        return "upd";
    }
    @RequestMapping("/del/{sid}")
    public String upd(@PathVariable Integer sid, Model md){
        boolean aa=itemsService.remove(new QueryWrapper<Items>().eq("i_sid",sid));
        boolean bb=optionService.remove(new QueryWrapper<Option>().eq("o_sid",sid));
        boolean cc=subjectService.remove(new QueryWrapper<Subject>().eq("sid",sid));
        if(aa&&bb&&cc){
             return "forward:/show/1/null";
        }
        md.addAttribute("pddel","删除失败");
        return "forward:/show/1/null";
    }
}